package com.oocl.venus.utils;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import org.apache.commons.configuration.Configuration;
import org.apache.commons.configuration.PropertiesConfiguration;

public class DbUtils {

	private static String url;
	private static String username;
	private static String password;
	private static String driverClass;

	static {
		try {
			Configuration config = new PropertiesConfiguration("datasource.properties");
			url = config.getString("url");
			username = config.getString("username");
			password = config.getString("password");
			driverClass = config.getString("driverClass");
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public static Connection connect() {
		Connection con = null;

		try {
			Class.forName(driverClass);
			con = DriverManager.getConnection(url, username, password);
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		} catch (SQLException e) {
			e.printStackTrace();
		}

		return con;
	}

	public static void free(Connection con, PreparedStatement pst, ResultSet rs) {
		try {
			if (rs != null) {
				rs.close();
			}
			if (pst != null) {
				pst.close();
			}
			if (con != null) {
				con.close();
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}
}
